package org.jeecg.modules.clues.jobs;


import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.crawler.other.SearchNewsCrawler;
import org.jeecg.modules.clues.entity.Clues;
import org.jeecg.modules.clues.service.ICluesService;
import org.jeecg.modules.crawlerpaper.entity.CrawlerInfo;
import org.jeecg.modules.crawlerpaper.service.ICrawlerInfoService;
import org.jeecg.modules.system.service.ISysDictService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 央视网线索爬虫
 */
@Slf4j
public class CCTVCluesJob implements Job {

    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改
     */
    private String parameter;

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }

    @Autowired
    private ICluesService cluesService;

    @SneakyThrows
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<String> keywords = cluesService.getAllKeyword();
        if (!CollectionUtils.isEmpty(keywords)) {
            List<Clues> crawlerInfoList = getCCTVNews(keywords, Integer.valueOf(parameter));
            cluesService.saveOrUpdateBatch(crawlerInfoList);
        }

    }


    /**
     * 获取cctv新闻
     *
     * @param keywords
     * @param page
     * @return
     * @throws Exception
     */
    public static List<Clues> getCCTVNews(List<String> keywords, Integer page) throws Exception {
        List<Clues> cluesList = new ArrayList<>();
        String  cctvUrl = "https://search.cctv.com/search.php?sort=relevance&type=web&vtime=&datepid=1&channel=";

        if (!CollectionUtils.isEmpty(keywords)) {
            for (int i = 1; i <= page; i++) {
                for (String keyword : keywords) {
                    List<Clues> iCrawlerInfoList = getCCTVNewsPageResponse(cctvUrl + "&page=" + i + "&qtext=" + keyword,keyword);
                    if (!CollectionUtils.isEmpty(iCrawlerInfoList)) {
                        cluesList.addAll(iCrawlerInfoList);
                    }
                }
            }
        }
        return cluesList;
    }


    /**
     * 央视新闻网
     * https://search.cctv.com/search.php?qtext=%E5%8D%97%E4%BA%AC&sort=relevance&type=web&vtime=&datepid=1&channel=&page=2
     *
     * @param
     * @return
     * @throws Exception
     */
    public static List<Clues> getCCTVNewsPageResponse(String url,String keyword) throws Exception {
        WebClient webClient = getClient();
        String result = getHtml(url, webClient);
        List<Clues> clueList = new ArrayList<>();
        Document doc = Jsoup.parse(result);
        Elements elementsByClass = doc.getElementsByClass("tright");
        for (Element byClass : elementsByClass) {
            Elements children = byClass.children();
            Clues clue = new Clues();
            for (Element child : children) {
                Elements hrefEle = child.getElementsByTag("span");
                if (hrefEle.size() > 0) {
                    String href = hrefEle.attr("lanmu1");
                    if (!StringUtils.isEmpty(href)) {

                        clue.setCrawlerUrl(href);
                        clue.setId("ysw"+getHrefId(href, "shtml"));
                    }

                }
                Elements titleEle = child.getElementsByTag("a");

                if (titleEle.size() > 0) {
                    String title = titleEle.text();
                    clue.setCluesTitle(title);

                }
                clue.setCluesSource(1);
                clue.setCrawlerSource(3);
                clue.setKeyword(keyword);
            }
            clueList.add(clue);
        }
        webClient.close();

        return clueList;
    }



    public  static WebClient  getClient(){
        WebClient  webClient = new WebClient(BrowserVersion.CHROME);

        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setActiveXNative(false);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        webClient.getOptions().setTimeout(30000);
        webClient.setJavaScriptTimeout(30000);
        webClient.getOptions().setRedirectEnabled(true);
        return  webClient;



    }


    public  static String  getHtml(String url, WebClient webClient) throws InterruptedException {
        HtmlPage page = null;
        try {
            webClient.getOptions().setUseInsecureSSL(true);
            page = webClient.getPage(url);
        } catch (Exception e) {
            e.printStackTrace();
            webClient.close();

        }

        //该方法阻塞线程
        webClient.waitForBackgroundJavaScript(30000);
        return   page.asXml();
    }

    public  static String  getHrefId(String  href,String endString){
        String[] split = href.split("/");
        return  split[split.length-1].replace("."+endString,"");
    }

}
